package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.CustomerAchivesType;
import com.mofang.powerdekorhelper.model.CustomerList;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.L;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.CustomerManagerView;

import java.util.ArrayList;
import java.util.List;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/8/24 0024.
 */

public class CustomerManagerPersenter extends BasePresent<CustomerManagerView> {

    RetrofitSerives retrofitSerives;
    Call<CustomerList> customerListCall;

    public void getCustomerList(String jsonParams) {

        L.i("CustomerManagerPersenter", "jsonParams----------->" + jsonParams);

        view.showProgress();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);

        customerListCall = retrofitSerives.getCustomerList(body);

        customerListCall.enqueue(new Callback<CustomerList>() {
            @Override
            public void onResponse(Call<CustomerList> call, Response<CustomerList> response) {
                if (response.isSuccessful()) {

                    if (response.body().getCode().equals("0")) {

                        if (response.body().getResult().getData() != null) {
                            view.setCustomerList(response.body());
                        } else {
                            view.showNullLayout();
                        }
                    } else {

                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {

                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<CustomerList> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    public void getarCustomerAchivesType() {

        CustomerAchivesType type = new CustomerAchivesType();

        List<CustomerAchivesType.Data> datas = new ArrayList<>();

        CustomerAchivesType.Data data1 = new CustomerAchivesType().new Data();
        data1.setId(0);
        data1.setTypeName("全部");
        datas.add(data1);

        CustomerAchivesType.Data data2 = new CustomerAchivesType().new Data();
        data2.setId(1);
        data2.setTypeName("新客户");
        datas.add(data2);


        CustomerAchivesType.Data data3 = new CustomerAchivesType().new Data();
        data3.setId(2);
        data3.setTypeName("订单客户");
        datas.add(data3);


        CustomerAchivesType.Data data4 = new CustomerAchivesType().new Data();
        data4.setId(3);
        data4.setTypeName("活动客户");
        datas.add(data4);

        type.setData(datas);

        view.setCustomerAchivesType(type);
    }

    @Override
    public void stopRequest() {

        L.e("CustomerManagerPersenter", "----->stopRequest");
    }
}
